package com.ruoyi.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.tws.domain.Bill;
import com.ruoyi.tws.service.IBillService;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 发票Controller
 * 
 * @author ruoyi
 * @date 2023-02-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/bill")
public class BillController extends BaseController {

    private final IBillService iBillService;


    /**
     * 查询发票列表
     */
    @PreAuthorize(hasPermi="tws:bill:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, Bill bill)
    {
        LambdaQueryWrapper<Bill> lqw = new LambdaQueryWrapper<Bill>();
        if (bill.getOrderCount() != null) {
            lqw.eq(Bill::getOrderCount, bill.getOrderCount());
        }
        if (bill.getTotalMoney() != null) {
            lqw.eq(Bill::getTotalMoney, bill.getTotalMoney());
        }
        if (StringUtils.isNotBlank(bill.getReceiptName())) {
            lqw.like(Bill::getReceiptName ,bill.getReceiptName());
        }
        if (StringUtils.isNotBlank(bill.getReceiptPhone())) {
            lqw.eq(Bill::getReceiptPhone, bill.getReceiptPhone());
        }
        if (StringUtils.isNotBlank(bill.getReceiptAddress())) {
            lqw.eq(Bill::getReceiptAddress, bill.getReceiptAddress());
        }
        if (bill.getBillUserId() != null) {
            lqw.eq(Bill::getBillUserId, bill.getBillUserId());
        }
        if (StringUtils.isNotBlank(bill.getBillUser())) {
            lqw.eq(Bill::getBillUser, bill.getBillUser());
        }
        if (bill.getBillType() != null) {
            lqw.eq(Bill::getBillType, bill.getBillType());
        }
        if (StringUtils.isNotBlank(bill.getBillNo())) {
            lqw.eq(Bill::getBillNo, bill.getBillNo());
        }
        if (bill.getBillDate() != null) {
            lqw.eq(Bill::getBillDate, bill.getBillDate());
        }
        if (bill.getStatus() != null) {
            lqw.eq(Bill::getStatus, bill.getStatus());
        }
        if (StringUtils.isNotBlank(bill.getCompanyName())) {
            lqw.like(Bill::getCompanyName ,bill.getCompanyName());
        }
        if (StringUtils.isNotBlank(bill.getTenantId())) {
            lqw.eq(Bill::getTenantId, bill.getTenantId());
        }
        lqw.orderByDesc(Bill::getCreateTime);
        IPage<Bill> list = iBillService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出发票列表
     */
    @PreAuthorize(hasPermi = "tws:bill:export")
    @Log(title = "发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bill bill) throws IOException
    {
        LambdaQueryWrapper<Bill> lqw = new LambdaQueryWrapper<Bill>(bill);
        List<Bill> list = iBillService.list(lqw);
        ExcelUtil<Bill> util = new ExcelUtil<Bill>(Bill. class);
        util.exportExcel(response, list, "bill");
    }

    /**
     * 获取发票详细信息
     */
    @PreAuthorize(hasPermi="tws:bill:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iBillService.getById(id));
    }

    /**
     * 新增发票
     */
    @PreAuthorize(hasPermi="tws:bill:add")
    @Log(title = "发票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bill bill) {
        return toAjax(iBillService.saveBill(bill) ? 1 : 0);
    }

    /**
     * 修改发票
     */
    @PreAuthorize(hasPermi="tws:bill:edit")
    @Log(title = "发票" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bill bill) {
        return toAjax(iBillService.updateBill(bill) ? 1 : 0);
    }

    //TODO 已经基本完成 暂不使用
//    /**
//     * 撤回开票
//     * @param bill
//     * @return
//     */
//    @PutMapping("/recall")
//    public AjaxResult recall(@RequestBody Bill bill){
//        return toAjax(iBillService.recallBill(bill) ? 1 : 0);
//    }

    /**
     * 删除发票
     */
    @PreAuthorize(hasPermi="tws:bill:remove")
    @Log(title = "发票", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iBillService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
